"""
Created on 21 mrt. 2012

@author: Daan
"""
import unittest
import os
import shutil

from unittests.stubs import stub_bases

from pCentralServer import CentralServer, Program
from pCentralServer.plugins import MachineManaging

TEST_DIR = os.path.join(os.path.dirname(__file__), "dat", "CentralServer")
UNITTEST_OUTPUT_DIR = os.path.join(TEST_DIR, "output")

class stub_Config(object):
    def __init__(self, values):
        self.values = values

    def __getattr__(self, item):
        if self.values.has_key(item):
            if isinstance(self.values[item], dict):
                return stub_Config(self.values[item])
            return self.values[item]
        raise KeyError(item)


#noinspection PyCallByClass,PyTypeChecker
class MachineManagingTester(unittest.TestCase):

    verbose = True

    def setUp(self):
        """
        DOCSTRING
        """

        if not os.path.isdir(UNITTEST_OUTPUT_DIR):
            os.makedirs(UNITTEST_OUTPUT_DIR)

        Program.Program.configStatic = stub_Config({
            "centralserver" : {
                "updateinterval"   : 0.1,
                "mmUpdateInterval" : 10
            }
        })

    def tearDown(self):
        """
        DOCSTRING
        """
        shutil.rmtree(UNITTEST_OUTPUT_DIR, True)

    def test_01_instantiation(self):
        """
        DOCSTRING
        """

        with stub_bases.stub_bases(CentralServer.CentralServer, MachineManaging.MachineManaging):
            testObject = CentralServer.CentralServer(UNITTEST_OUTPUT_DIR)

            self.assertTrue(isinstance(testObject, CentralServer.CentralServer))
            self.assertTrue(isinstance(testObject, MachineManaging.MachineManaging))

    def test_02a_registerMachine(self):
        """
        DOCSTRING
        """

        with stub_bases.stub_bases(CentralServer.CentralServer, MachineManaging.MachineManaging):
            testObject = CentralServer.CentralServer(UNITTEST_OUTPUT_DIR)

            self.assertTrue(MachineManaging.MachineManaging.registerMachine(testObject, ("unittest_machine1", "localhost")))
            self.assertTrue(MachineManaging.MachineManaging.registerMachine(testObject, ("unittest_machine2", "localhost")))

    def test_03a_retrieveMachines(self):
        """
        DOCSTRING
        """

        with stub_bases.stub_bases(CentralServer.CentralServer, MachineManaging.MachineManaging):
            testObject = CentralServer.CentralServer(UNITTEST_OUTPUT_DIR)

            self.assertEqual(MachineManaging.MachineManaging.retrieveMachines(testObject), [])

            self.assertTrue(MachineManaging.MachineManaging.registerMachine(testObject, ("unittest_machine1", "localhost")))
            self.assertTrue(MachineManaging.MachineManaging.registerMachine(testObject, ("unittest_machine2", "localhost")))

            self.assertEqual(MachineManaging.MachineManaging.retrieveMachines(testObject), [("unittest_machine1", "localhost"), ("unittest_machine2", "localhost")])

if __name__ == "__main__":
    unittest.main()